package com.jie.jie.Fanshe;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.jie.jie.R;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class FansheActivity extends AppCompatActivity {


    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fanshe);
        imageView = (ImageView) findViewById(R.id.iv);
    }

    public void click(View view)  {
        switch (view.getId()) {
            case R.id.btn_1:
                try {
                    Class aClass = ReflectionTest.class;
                    Class[] classes = {int.class, String.class};
                    Method method = aClass.getDeclaredMethod("show1", classes);
                    //getDeclaredMethod可以得到所有的方法包括私有
                    //getMethod 只能得到公有方法
                    method.setAccessible(true);
                    method.invoke(aClass.newInstance(), 1, "sssss");

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
                break;
            case R.id.btn_2:
                try {
                    //通过包名和类名获取类
                    Class<?> aClass = Class.forName("com.jie.jie.Fanshe.ReflectionTest");
                    //获取带参数的类的对象
                    Constructor<?> constructor = aClass.getConstructor(Context.class);
                    Class<?>[] interfaces = aClass.getInterfaces();//获取接口
                    Class<?> superclass = aClass.getSuperclass();//获取对象父类
                    Type genericSuperclass = aClass.getGenericSuperclass();//获取带泛型的父类
//                    genericSuperclass.getActualTypeArguments()获取父类泛型
//                    Type genericSuperclass = (ParameterizedType)aClass.getGenericSuperclass().getActualTypeArguments()[i] 获取父类泛型

                    Object o = constructor.newInstance(this);
                    //获取所有类的对象
                    Method show2 = aClass.getDeclaredMethod("show2");
                    show2.setAccessible(true);//可以访问private域
                    show2.invoke(o);//执行方法
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
                break;
            case R.id.btn_3:
                try {
                    Class<?> aClass = Class.forName("com.jie.jie.Fanshe.ReflectionTest");
                    Field str1 = aClass.getDeclaredField("str1");
                    System.out.println(str1.getName());//字段名
                    System.out.println(str1.getGenericType());//字段类型

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
                break;
            case R.id.btn_4:
                try {
                    Class<?> aClass = Class.forName("com.jie.jie.Fanshe.ReflectionTest");
                    Constructor<?> constructor = aClass.getConstructor(Context.class);
                    Object o = constructor.newInstance(FansheActivity.this);
                    Method show2s = aClass.getDeclaredMethod("show2");
                    show2s.setAccessible(true);
                    Field str2 = aClass.getField("str2");
                    str2.set(o, "hhhhhhhhhh");
                    show2s.invoke(o);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
                break;
            case R.id.btn5:
                int drawableId = CPResourceUtil.getDrawableId(this, "ic_aa");
                imageView.setImageResource(drawableId);
                break;
        }
    }
}
